/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui;

import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JEditorPane;

import dgbgui.modeldocument.ModelDrivenWorkspace;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.primitives.Concept;
import dgbgui.modeldocument.primitives.Conceptron;
import dgbgui.selection.SelectionManager;

public class ModelDrivenInspector implements Observer {
    
    
    public ModelDrivenInspector() {
        
        // Create the text area that will be sued to display the 
        // inspector information
        m_textArea = new JEditorPane();
        m_textArea.setEditable(false);
    }
    
    public void update(Observable source, Object data) {
        
        if ( source instanceof SelectionManager ) {
            assert ( data instanceof SelectionManager.ObservableData );
            // We're not concerned with the actual added/removed selection,
            // we just get the current selection and display that
            

            if (SelectionManager.getInstance().size(Conceptron.class) == 1  ) { 

                Set<Object> selection = SelectionManager.getInstance().getSelection(Conceptron.class);
                Conceptron c = (Conceptron)selection.iterator().next();
                if ( c instanceof Concept) {
                    Concept concept = (Concept)(c);
                    if (ModelDrivenWorkspace.getInstance().getTarget() == concept) {
                        
                        // The model-driven query string
                        Query targetQuery = ModelDrivenWorkspace.getInstance().getTargetQuery();
                        if ( targetQuery != null ) {
                            m_textArea.setText("Target query string: " + targetQuery.getDefinition().getdgbQLString());
                        } else {
                            m_textArea.setText("No target query");
                        }
                        
                    }
                }
            }   
            else {
                m_textArea.setText("");
            }
        }
    }
    
    public JComponent getComponent() {
        return m_textArea;
    }

    JEditorPane m_textArea;
    
}
